﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.UI.HtmlControls;

/// <summary>
/// This class acts as a base page for most of the
/// web pages for the website.  The purpose of this was
/// to consolidate code that needs to run on every page.
/// </summary>
public class BasePage : System.Web.UI.Page
{
    private bool _canEditPage = false;
    private bool _canDeletePage = false;

    public bool CanEditPage
    {
        get
        {
            return _canEditPage;
        }
    }

    public bool CanDeletePage
    {
        get
        {
            return _canDeletePage;
        }
    }

    /// <summary>
    /// Needed to add an event handler for when the page
    /// initializes.
    /// </summary>
    public BasePage()
    {
        PreInit += new EventHandler(BasePage_PreInit);
        Init += new EventHandler(BasePage_Init);
    }

    /// <summary>
    /// Needed to make sure the page theme is set to the 
    /// current theme for the web site.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void BasePage_PreInit(object sender, EventArgs e)
    {
        Asah.SiteWorx.SiteMap.PageInfo pageInfo = Asah.SiteWorx.Site.GetSite().CurrentPage;
        if (pageInfo != null)
        {
            if (!User.IsInRole("Administrator")) // Need to check to see if user is logged in as administrator. 
            {
                // If not logged in as administrator then we need to check if page is unlisted.
                if (pageInfo.Visibility == Asah.SiteWorx.SiteMap.PageVisibility.Unlisted)
                {
                    Response.Status = "404 OK";
                    Response.End();
                }
            }
            _keywords = _keywords = Regex.Replace(pageInfo.MetaKeywords, "\\s+", " ");
            _description = Regex.Replace(pageInfo.MetaDescription, "\\s+", " ");
            _canEditPage = Asah.SiteWorx.SiteMap.Page.CanEditPage();
            _canDeletePage = Asah.SiteWorx.SiteMap.Page.CanDeletePage(pageInfo.FileName);
            Page.Title = pageInfo.Title;
            Page.MasterPageFile = pageInfo.MasterPage;
        }
        Page.Theme = Asah.SiteWorx.Site.GetSite().Theme;
    }

    /// <summary>
    /// This routine runs when the page is initialized.  
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void BasePage_Init(object sender, EventArgs e)
    {
        ApplyMetaTags();
    }

    #region Meta Keywords & Description

    private string _keywords;
    private string _description;
    
    /// <summary>
    /// Keywords property contains certain keywords
    /// of the current page.  This is used typically
    /// for search engines.
    /// </summary>
    public string MetaKeywords
    {
        get
        {
            return _keywords;
        }
    }

    /// <summary>
    /// Description property contains the description
    /// of the current page.  This is used typically
    /// for search engines.
    /// </summary>
    public string MetaDescription
    {
        get
        {
            return _description;
        }
    }

    /// <summary>
    /// This routine injects the proper header meta keywords 
    /// and meta description tags to the current page.
    /// </summary>
    private void ApplyMetaTags()
    {
        if (!String.IsNullOrEmpty(MetaKeywords))
        {
            HtmlMeta metaTag = new HtmlMeta();
            metaTag.Name = "keywords";
            metaTag.Content = MetaKeywords;
            Header.Controls.Add(metaTag);
        }
        if (!String.IsNullOrEmpty(MetaDescription))
        {
            HtmlMeta metaTag = new HtmlMeta();
            metaTag.Name = "description";
            metaTag.Content = MetaDescription;
            Header.Controls.Add(metaTag);
        }
    }
        
    #endregion

    #region ViewState Provider Service 

    /// <summary>
    /// Saves any view and control state to appropriate viewstate provider.
    /// This method shields the client from viewstate key generation issues.
    /// </summary>
    /// <param name="viewState"></param>
    protected override void SavePageStateToPersistenceMedium(object viewState)
    {
        // Make up a unique name
        string random = new Random().Next(0, int.MaxValue).ToString();
        string name = "ACTION_" + random + "_" + Request.UserHostAddress + "_" + DateTime.Now.Ticks.ToString();

        Asah.Web.ViewState.Provider.ViewStateProviderService.SavePageState(name, viewState);
        ClientScript.RegisterHiddenField("__VIEWSTATE_KEY", name);
    }

    protected override object LoadPageStateFromPersistenceMedium()
    {
        string name = Request.Form["__VIEWSTATE_KEY"];
        return Asah.Web.ViewState.Provider.ViewStateProviderService.LoadPageState(name);
    }

    #endregion
}
